package com.bleeth.flow.step.other;


import com.bleeth.flow.core.common.APlugin;
import com.bleeth.flow.core.common.PluginAnnotation;
import com.bleeth.flow.core.common.PluginTypeEnum;
import com.bleeth.flow.core.util.SparkUtil;
import com.jd.platform.async.wrapper.WorkerWrapper;
import lombok.Data;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.io.Serializable;
import java.util.Map;

/**
 * @author ：Bleeth
 * @date ：2021-08-05
 * @description：SQL万能插件
 */
@Data
@PluginAnnotation(name = "SQL插件",
        type = PluginTypeEnum.OTHER,
        description = "",
        id = "SQLPlugin")
public class SQLPlugin extends APlugin implements Serializable {

    private static final long serialVersionUID = -5878384185017490024L;

    private String tableName;

    private String sql;


    @Override
    public Dataset<Row> action(Dataset<Row> param, Map<String, WorkerWrapper> allWrappers) {
        super.action(param, allWrappers);


        String fromPluginName = fromList.get(0);
        WorkerWrapper fromWrapper = allWrappers.get(fromPluginName);
        Dataset<Row> ds = (Dataset<Row>) fromWrapper.getWorkResult().getResult();
        ds.registerTempTable(tableName);

        SparkSession spark = SparkUtil.getSparkInstance();
        ds = spark.sql(sql);

        return ds;
    }


}
